package com.onix.scalecamerapreview.translate.OCR;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.Nullable;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import com.github.chrisbanes.photoview.PhotoView;
import com.onix.scalecamerapreview.translate.msOcr.Line;
import com.onix.scalecamerapreview.translate.msOcr.Region;

import java.util.List;


public class MsBitmapView extends PhotoView {
    private Region regionsBean;
    private final Paint rectPaint;
    private final Paint mTextPaint;
    private final TextPaint tp;

    public MsBitmapView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //区域
        rectPaint = new Paint();
        rectPaint.setColor(Color.RED);
        rectPaint.setStyle(Paint.Style.STROKE);
        rectPaint.setStrokeWidth(2);

        mTextPaint = new Paint();
        mTextPaint.setColor(Color.BLACK);
        mTextPaint.setTextSize(40);

        tp = new TextPaint();
        tp.setColor(Color.BLACK);
        tp.setStyle(Paint.Style.FILL);
        tp.setTextSize(40);
    }


    public void show(Region regionsBean){
        this.regionsBean = regionsBean;
        postInvalidate();
    }
/*
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(regionsBean!=null){
            String boundingBox = regionsBean.getBoundingBox();
            String[] split = boundingBox.split(",");
            RectF rect = new RectF(Float.valueOf(split[0]),Float.valueOf(split[1]),Float.valueOf(split[0])+Float.valueOf(split[2]),
                    Float.valueOf(split[1])+Float.valueOf(split[3]));
            rectPaint.setStyle(Paint.Style.STROKE);
            rectPaint.setColor(Color.RED);
            canvas.drawRect(rect,rectPaint);

            for (MsOcrBean.RegionsBean.LinesBean linesBean : regionsBean.getLines()) {
                String box = linesBean.getBoundingBox();
                String[] boxsplit = box.split(",");
                RectF linerect = new RectF(Float.valueOf(boxsplit[0]),Float.valueOf(boxsplit[1]),Float.valueOf(boxsplit[0])+Float.valueOf(boxsplit[2]),
                        Float.valueOf(boxsplit[1])+Float.valueOf(boxsplit[3]));
                rectPaint.setStyle(Paint.Style.FILL);
                rectPaint.setColor(Color.WHITE);
                canvas.drawRect(linerect,rectPaint);
            }

            canvas.save();
            canvas.translate(rect.left,rect.top);
//            String content = "At that time\n" +
//                    "Like you\n" +
//                    "It's not because you have a house and a car.\n" +
//                    "But because\n" +
//                    "That afternoon\n" +
//                    "Sunshine is right.\n" +
//                    "You're wearing an open dress.";
            String content = "At that time, I liked you not because you had a house and a car, but because the sunshine that afternoon happened to make you wear an open dress.";
            StaticLayout myStaticLayout = new StaticLayout(content, tp, (int) rect.width(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
            int height = myStaticLayout.getHeight();
            Log.d("AAA","height "+height +" rect "+rect.height());
            if(height>rect.height()){


            }else if(height<rect.height()){

                while (height+tp.getTextSize()<rect.height()){
                    tp.setTextSize(tp.getTextSize()+1);
                    myStaticLayout = new StaticLayout(content, tp, (int) rect.width(), Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
                    height = myStaticLayout.getHeight();
                }
                if(height!=rect.height()){
                    float v = (rect.height() - height) / height;
                    myStaticLayout = new StaticLayout(content, tp, (int) rect.width(), Layout.Alignment.ALIGN_NORMAL, 1.0f+v, 0.0f, false);
                }
            }
            Log.d("AAA","myStaticLayout.getHeight() "+height);
            myStaticLayout.draw(canvas);

            canvas.restore();


        }
    }*/

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(regionsBean!=null){
            for (Line line : regionsBean.getLines()) {
                String boundingBox = line.getBoundingBox();
                String[] split = boundingBox.split(",");
                RectF rect = new RectF(Float.valueOf(split[0]),Float.valueOf(split[1]),Float.valueOf(split[0])+Float.valueOf(split[2]),Float.valueOf(split[1])+Float.valueOf(split[3]));
                canvas.drawRect(rect,rectPaint);
            }

          /*  List<MsOcrBean.Region.LinesBean.WordsBean> words = linesBean.getWords();
            StringBuilder stringBuilder = new StringBuilder();
            for (MsOcrBean.Region.LinesBean.WordsBean word : words) {
                stringBuilder.append(word.getText());
            }
            if(stringBuilder.toString().equals("并不是因为你有房有车")) {
                rectPaint.setStyle(Paint.Style.FILL);
                rectPaint.setColor(Color.WHITE);
//                    canvas.drawRect(rect,rectPaint);
                for (MsOcrBean.Region.LinesBean.WordsBean wordsBean : linesBean.getWords()) {
                    String wordsBox = wordsBean.getBoundingBox();
                    String[] w = wordsBox.split(",");
                    RectF wordRectf = new RectF(Float.valueOf(w[0]),Float.valueOf(w[1]),Float.valueOf(w[0])+Float.valueOf(w[2]),Float.valueOf(w[1])+Float.valueOf(w[3]));
                    Bitmap dstArea = getDstArea(wordRectf);
                    canvas.drawBitmap(dstArea,new Rect(0,0,dstArea.getWidth(),dstArea.getHeight()),wordRectf,rectPaint);
                }

                String content = "not because you have room and car";
                Rect textBound = new Rect();
                mTextPaint.getTextBounds(content, 0, content.length(), textBound);



                int availableWidth = (int) rect.width();
                int currentWdith = textBound.width();
                float cTextSize = mTextPaint.getTextSize();
                Log.d("AAA","cTextSize "+cTextSize +"currentWdith "+currentWdith+" - availableWidth "+availableWidth);
                while (currentWdith>availableWidth){
                    cTextSize = cTextSize-0.5f;
                    mTextPaint.setTextSize(cTextSize);//这里传入的单位是px
                    mTextPaint.getTextBounds(content, 0, content.length(), textBound);
                    currentWdith = textBound.width();
                    Log.d("AAA","currentWdith "+currentWdith);
                }
                cTextSize = cTextSize+0.5f;
                mTextPaint.setTextSize(cTextSize);
                Log.d("AAA","cTextSize之后 "+cTextSize);

                Paint.FontMetricsInt anInt = mTextPaint.getFontMetricsInt();
                //字体基线
                int dy = (anInt.bottom - anInt.top) / 2 - anInt.bottom;
                canvas.drawText(content, rect.left,  dy+rect.top+(rect.bottom-rect.top)/2, mTextPaint);
            }else {
                rectPaint.setStyle(Paint.Style.STROKE);
                rectPaint.setColor(Color.BLUE);
                canvas.drawRect(rect,rectPaint);
            }*/
        }

    }

    public  Bitmap getDstArea(RectF rect) {
        BitmapDrawable drawable = (BitmapDrawable) this.getDrawable();
        Bitmap dstArea = Bitmap.createBitmap(drawable.getBitmap(),(int)rect.left,(int)rect.top,(int)rect.width(),(int)rect.height(),null,false);
        return Fastblur.blur(getContext(),dstArea);
    }
}
